package com.flyan.game.motafx.effect.item;

import com.almasb.fxgl.entity.Entity;
import com.flyan.game.motafx.component.BackpackComponent;
import com.flyan.game.motafx.core.enums.EntityTypeEnum;
import com.flyan.game.motafx.core.panel.GameInfoPanel;
import com.flyan.game.motafx.core.util.console.ConsoleUtils;
import com.flyan.game.motafx.core.util.entity.EntityUtils;
import com.flyan.game.motafx.effect.BaseEffect;
import com.flyan.game.motafx.entity.item.ItemEntityInfo;
import javafx.util.Duration;
import org.jetbrains.annotations.NotNull;

/**
 * 将道具收进背包效果
 * <p></p>
 * 若拾取单位携带有背包，则自动加入背包
 *
 * @author flyan
 * @version 1.0
 * @email flyan_by_chenu@sina.com
 * @since 2025/2/7
 */
public class PutInBackpackEffect extends BaseEffect {

    public PutInBackpackEffect() {
        super(Duration.seconds(0.1));
    }

    @Override
    public void onStart(@NotNull Entity entity) {
        if (entity.hasComponent(BackpackComponent.class)) { // 有背包才能收
            BackpackComponent backpackComponent = entity.getComponent(BackpackComponent.class);
            EntityTypeEnum itemType = (EntityTypeEnum) triggerEntity.getType();
            ItemEntityInfo itemInfo = EntityUtils.getEntityInfo(triggerEntity, ItemEntityInfo.class);
            backpackComponent.getBackpack().addItem(itemType, itemInfo.getAmount());
            GameInfoPanel.redraw();

            ConsoleUtils.println("%s捡起物品%s(x%d)".formatted(
                    ((EntityTypeEnum)entity.getType()).getName(),
                    ((EntityTypeEnum)itemType).getName(),
                    itemInfo.getAmount()));
        }
    }

    @Override
    public void onEnd(@NotNull Entity entity) {

    }

}
